import requests
import json
import random
from copy import copy
from CloudEngine.core.VTActor import VTActor
from CloudEngine.Render.GyLoader import GyLoader


def read_gy(service, url, Params={}):
    total = 0
    response = requests.get(url)

    for obj_str in json.loads(response.content):
        vertexes = []
        loader = GyLoader(obj_str)
        tmp = copy(loader.vertexes_map)
        for key, value in tmp.items():
            vertexes.append({"id": value[0], "XYZ": value})
        actor = VTActor(loader.poly_data, {
            "outlines": loader.edgeData,
            "color": [random.random(), random.random(), random.random(), 255],
            # "vertexes": vertexes
        })
        # 处理渲染模式
        if Params.get("RenderType"):
            actor.set_render_type(Params.get("RenderType"))
        else:
            actor.set_render_type("SURFACE")
        total = total + actor.total_triangles()
        service.model_add(actor)
    print("总的三角面数:", total)
